package cn.circle.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;



import cn.circle.commom.controller.BackendBaseController;
import cn.circle.system.model.OrderDeatil;
//import cn.circle.commom.model.Apartment;
import cn.circle.system.model.OrderInfo;
import cn.circle.system.service.IOrderDeatilService;
import cn.circle.system.service.IOrderInfoService;
import cn.circle.system.pagemodel.DataGrid;
import cn.circle.system.pagemodel.PageHelper;
import cn.circle.system.pagemodel.ReturnInfo;

/**
 * 
 * @author 
 * 时间：
 */
@Controller
@EnableWebMvc
@RequestMapping("/manage/order")
public class OrderController extends BackendBaseController{
	private static final Log log = LogFactory.getLog(OrderController.class);

	@Autowired
	private IOrderInfoService orderInfoService;
	
	@Autowired
	private IOrderDeatilService orderDeatilService;

	/**
	 * 跳转至 syscustomer页
	 */
	@RequestMapping(value = "/index")
	public String index(HttpServletRequest request) {

		return super.index("/WEB-INF/pages/system/order/order_list.jsp", request);
		
	}
	@ResponseBody
	@RequestMapping(value = "/dataGrid")
	public DataGrid dataGrid(@ModelAttribute OrderInfo o, PageHelper ph,String begin_time,String end_time,HttpServletRequest request){

		//返回JSON数据
		ph.setSort("payTime");
		ph.setOrder("desc");
		System.out.println("时间"+end_time);
/*        DataGrid dg = orderInfoService.datagrid(ph,o,begin_time,end_time,getUserDepId(request));*/
		  DataGrid dg = orderInfoService.datagrid(ph,o,begin_time,end_time);
		return dg;
	} 
	/**
	 * 添加页面
	 */
	@RequestMapping(value = "/add")
	public String add(Map<String, Object> map) {
		map.put("saveUrl", "/manage/order/addSave");
		return "/WEB-INF/pages/system/order/order_au.jsp";
	}
	/**
	 * 添加保存
	 */
	@ResponseBody
	@RequestMapping(value ="/addSave")
	public ReturnInfo addSave(@Valid OrderInfo o,BindingResult b,ReturnInfo result){
		if(b.getErrorCount() >0){
			for(FieldError error : b.getFieldErrors()){
				result.setMsg(error.getField() + ""+error.getDefaultMessage());
			}
			result.setSuccess(1);
		}else{
			if (o != null) {
				try {
					orderInfoService.save(o);
				} catch (Exception e) {
					log.info(""+e.getMessage());
					result.setMsg("保存失败！");
					result.setSuccess(1);
				}
			}
		}
		return result;
	}
	/**
	 * 跳转至 apartment_au页
	 */
	@RequestMapping(value = "/edit")
	public String edit(String id, Map<String, Object> map) {
		map.put("saveUrl", "/manage/apartment/editSave");
		if (id != null) {
			//List<Apartment> model = orderInfoService.getOne(id);
			//map.put("model", model.get(0));
		}
		return "/WEB-INF/pages/commom/apartment/apartment_au.jsp";
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping(value = "/editSave")
	public ReturnInfo editSave(@Valid OrderInfo o, BindingResult b, ReturnInfo result) {
		if (b.getErrorCount() > 0) { // 数据校验
			log.info("数据校验出错！");
			for (FieldError error : b.getFieldErrors()) {
				log.info(error.getField() + ":" + error.getDefaultMessage());
				result.setMsg(error.getField() + "" + error.getDefaultMessage());
			}
			result.setSuccess(1);
		} else {
			if (o != null) {
				try {
					orderInfoService.update(o);
				} catch (Exception e) {
					log.info("修改异常" + e.getMessage());
					result.setMsg("修改失败");
					result.setSuccess(1);
				}

			}
		}

		return result;
	}

	/**
	 * 物理删除,从数据库中删除
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
	public ReturnInfo delete(String ids, ReturnInfo result) {
		if (ids != null) {
			try {
				orderInfoService.deleteAll(ids);
			} catch (Exception e) {
				log.info("删除异常" + e.getMessage());
				result.setMsg("删除失败！");
				result.setSuccess(1);
			}
		}

		return result;
	}

	/**
	 * 移除,逻辑删除，将状态修改为0
	 */
	@ResponseBody
	@RequestMapping(value = "/remove")
	public ReturnInfo remove(String ids, ReturnInfo result) {

		if (ids != null) {
			try {
				orderInfoService.removeAll(ids);
			} catch (Exception e) {
				log.info("删除异常" + e.getMessage());
				result.setMsg("删除失败！");
				result.setSuccess(1);
			}
		}

		return result;
	}
	@ResponseBody
	@RequestMapping(value = "/updateStatus")
	public ReturnInfo updateStatus(String ids, ReturnInfo result) {
		
		if (ids != null) {
			try {
				orderInfoService.updateStatus(ids);
			} catch (Exception e) {
				log.info("删除异常" + e.getMessage());
				result.setMsg("删除失败！");
				result.setSuccess(1);
			}
		}

		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getOrderDetail")
	public ReturnInfo getOrderDetail(String orderNo, ReturnInfo result) {
		
		if (orderNo != null) {
			List<OrderDeatil> OrderDeatil = null;
			try {

				OrderDeatil = orderDeatilService.getOrderDeatil(orderNo);
				result.setRows(OrderDeatil);
			} catch (Exception e) {
				log.info("异常" + e.getMessage());
				result.setMsg("失败！");
				result.setSuccess(1);
			}
		}

		return result;
	}
	
	@RequestMapping(value = "/getOrderFrom")
	public String getOrderFrom(HttpServletRequest request, String orderNo, Model model) {
		
		model.addAttribute("orderNo", orderNo);
		return  "/WEB-INF/pages/system/order/orderForm.jsp";
	}
	
}
